# This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PySide2.QtWidgets import QApplication, QDialog, QScrollArea
from PySide2.QtCore import QFile, Qt
from PySide2.QtUiTools import QUiLoader
import numpy as np
import math

from mogi import mogi_vertical
from mogi import mogi_horizontal

class Dialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.load_ui()
        self.view1=QScrollArea()
        self.view2= QScrollArea()
    def load_ui(self):
        loader = QUiLoader()
        path = Path(__file__).resolve().parent / "form.ui"
        ui_file = QFile(str(path))
        ui_file.open(QFile.ReadOnly)
        self.ui=loader.load(ui_file, self)
        self.ui.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
        ui_file.close()
        self.ui.show()
        self.ui.start.clicked.connect(self.Start)
        self.ui.remove.clicked.connect(self.clear)
        self.ui.readme.clicked.connect(self.ReadMe)
    def Start(self):
        """计算"""

        a=float(self.ui.t1.text())#半径
        d = float(self.ui.t2.text())#深度
        sigma = float(self.ui.t3.text())#泊松比
        delta_p = float(self.ui.t4.text())#超压
        g = float(self.ui.t5.text())#剪切模量
        x=float(self.ui.t6.text())#绘图半径

        self.image1 = plt.figure()
        """计算三维垂直位移并绘图"""
        x = int(x * 10)  # 单位0.1km
        image_V = np.zeros((2 * x, 2 * x))
        image_H = np.zeros((2 * x, 2 * x))
        for i in range(2 * x):
            for j in range(2 * x):
                L_dis = math.sqrt((x - i) * (x - i) + (x - j) * (x - j)) * 100
                image_V[i, j] = mogi_vertical(L_dis, a, d, g, delta_p, sigma) * 1000
                image_H[i, j] = mogi_horizontal(L_dis, a, d, g, delta_p, sigma) * 1000
        plt.imshow(image_V, origin="lower", cmap="jet_r")
        plt.title("垂直位移图(mm)")
        plt.xlabel("距离(km)")
        plt.ylabel("距离(km)")
        plt.xticks(np.arange(0, 2 * x+0.0000001, (2 * x / 8)), abs(np.arange(-x / 10, x / 10+0.00000001, (2 * x / 80))))
        plt.yticks(np.arange(0, 2 * x+0.0000001, (2 * x / 8)), abs(np.arange(-x / 10, x / 10+0.00000001, (2 * x / 80))))
        plt.colorbar()
        plt.tight_layout()
        #self.ui.VL1.removeWidget(self.view1)
        self.ui.VL1.removeWidget(self.view1)
        self.view1 = FigureCanvas(self.image1)
        self.ui.VL1.addWidget(self.view1)
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        self.image2=plt.figure()
        """三维水平位移并绘图"""
        plt.imshow(image_H, origin="lower", cmap="jet_r")
        plt.title("水平位移图(mm)")
        plt.xlabel("距离(km)")
        plt.ylabel("距离(km)")
        plt.xticks(np.arange(0, 2 * x + 0.0000001, (2 * x / 8)),abs(np.arange(-x / 10, x / 10 + 0.00000001, (2 * x / 80))))
        plt.yticks(np.arange(0, 2 * x + 0.0000001, (2 * x / 8)), abs(np.arange(-x / 10, x / 10 + 0.00000001, (2 * x / 80))))
        plt.colorbar()
        plt.tight_layout()
        self.ui.VL2.removeWidget(self.view2)
        self.view2 = FigureCanvas(self.image2)
        self.ui.VL2.addWidget(self.view2)
    def clear(self):
        """清空文本框"""
        self.ui.t1.setText("")
        self.ui.t2.setText("")
        self.ui.t3.setText("")
        self.ui.t4.setText("")
        self.ui.t5.setText("")
        self.ui.t6.setText("")

    def ReadMe(self):
        """网页跳转到软件说明"""
        os.startfile("https://southern-saxophone-c79.notion.site/Mogi-Model-97058147ecba410e82589b291fc3bf39")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = Dialog()
    sys.exit(app.exec_())
